/*
游戏场景中单个玩家节点处理逻辑
*/
var confige = require("confige");

cc.Class({
    extends: cc.Component,

    properties: {
    },

    // use this for initialization
    onLoad : function () {},

    onInit:function(){
        this.initUserInfo();
        this.initHandCard();
        this.initStatus();
    },
    //初始化玩家信息相关接口
    initUserInfo:function(){
        this.userInfoNode = this.node.getChildByName("userInfoNode");
        this.userHead = this.userInfoNode.getChildByName("head").getComponent("cc.Sprite");
        this.userNick = this.userInfoNode.getChildByName("nick").getComponent("cc.Label");
        this.userScore = this.userInfoNode.getChildByName("score").getComponent("cc.Label");
        this.curScore = 0;
        //设置头像，传入头像图片链接
        this.setHead = function(url){
            window.DKTools.setFrameWithUrl(url,this.userHead);
        };
        //设置昵称，传入昵称
        this.setNick = function(nick){
            this.userNick.string = this.subNameString(nick,6);
        };
        //设置分数，传入分数
        this.setScore = function(score){
            this.curScore = Math.round(parseInt(score))
            this.userScore.string = Math.round(parseInt(score));
        };
        //获取分数
        this.getScore = function(){
            return this.curScore;
        };
        this.resetUserInfo = function(){
            this.setNick("");
            this.setScore("");
            this.userHead.spriteFrame = null;
        };
    },
    //根据长度截取字符串长度 末尾加...
    subNameString:function(str,isLenght){
        var realLength = 0;
        var len = str.length;
        var subLen = 0;
        for (var i = 0; i < len; i++) {
            var charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128) 
               realLength += 1;
            else
               realLength += 2;
            subLen += 1;
            if(realLength >= isLenght)
            {
                break;
            }
        }
        var str1 = "";
        if(realLength < len)
        {
            str1 = str.substring(0,subLen)+ "...";
        }
        else
        {
            str1 = str;
        }
        
        return str1;
    },
    initHandCard:function(){
        this.handCardNode = this.node.getChildByName("handCard");
        this.cardNode = this.handCardNode.getChildByName("cardNode");
        this.cardBackNode = this.cardNode.getChildByName("cardBackNode");
        this.cardTypeNode = this.handCardNode.getChildByName("cardType");

        this.cardFrameMap = {};
        this.cardBackFrame = null;
        
        this.cards = new Array(5);
        this.cardsNum = new Array(5);
        this.cards[0] = this.cardNode.getChildByName("card1")
        this.cards[1] = this.cardNode.getChildByName("card2")
        this.cards[2] = this.cardNode.getChildByName("card3")
        this.cards[3] = this.cardNode.getChildByName("card4")
        this.cards[4] = this.cardNode.getChildByName("card5")
        this.cardsNum[0] = this.cardsNum[1] = this.cardsNum[2] = this.cardsNum[3] = this.cardsNum[4] = this.cardsNum[5] = 0;
        

        this.cardsBack = new Array(5);
        
        this.cardsBack[0] = this.cardBackNode.getChildByName("cardBack1")
        this.cardsBack[1] = this.cardBackNode.getChildByName("cardBack2")
        this.cardsBack[2] = this.cardBackNode.getChildByName("cardBack3")
        this.cardsBack[3] = this.cardBackNode.getChildByName("cardBack4")
        this.cardsBack[4] = this.cardBackNode.getChildByName("cardBack5")
        
        this.cardType = this.cardTypeNode.getChildByName("type").getComponent("cc.Sprite");
        this.showCardType = function(type,isShowB){
            this.cardTypeNode.active = true;
            if(confige.roomData.roomType == "PaiGow" || confige.roomData.roomType == "PaiGowBalcony"){
                if(type>10)
                    type = 10;
                if(isShowB)
                    this.cardType.spriteFrame = confige.paiGowTypeFrameMapB[type];
                else
                    this.cardType.spriteFrame = confige.paiGowTypeFrameMap[type];
            }
            if(confige.roomData.roomType == "sanKung")
                this.cardType.spriteFrame = confige.sanKungTypeFrameMap[type];
        };

        this.hideCardType = function(){
            this.cardBackNode.active = false;
        };
        this.resetCard();
    },

    initStatus:function(){
        this.statusNode = this.node.getChildByName("statusNode");
        this.statusList = {};
        this.statusList["isReady"] = this.statusNode.getChildByName("statusReady");
        this.statusList["isRob"] = this.statusNode.getChildByName("statusRob");
        this.statusList["noRob"] = this.statusNode.getChildByName("statusNoRob");
        this.statusList["robMore"] = this.statusNode.getChildByName("statusRobMore");
        this.statusList["betNum"] = this.statusNode.getChildByName("betNum");
        this.statusList["banker"] = this.statusNode.getChildByName("statusBanker");
        this.leaveNode = this.node.getChildByName("leaveNode");
        this.sayAni = this.node.getChildByName("sayAni");
        this.sayWord = this.node.getChildByName("sayWord");
        this.sayWordTextNode = this.sayWord.getChildByName("text");
        this.sayWordText = this.sayWordTextNode.getComponent("cc.Label");
        this.faceItem = this.node.getChildByName("faceItem");

        //隐藏所有状态
        this.resetStatus = function(){
            this.statusList["isReady"].active = false;
            this.statusList["isRob"].active = false;
            this.statusList["noRob"].active = false;
            this.statusList["robMore"].active = false;
            this.statusList["betNum"].active = false;
        };
        //显示对应的状态:

        this.setReady = function(){
            this.resetStatus();
            this.statusList["isReady"].active = true;
        };
        this.hideReady = function(){
            this.statusList["isReady"].active = false;
        };
        this.setRob = function(){
            console.log("setRob!!!!!!!");
            this.resetStatus();
            this.statusList["isRob"].active = true;
        };
        this.setNoRob = function(){
            this.resetStatus();
            this.statusList["noRob"].active = true;
        };
        this.setRobMore = function(robNum){
            this.resetStatus();
            this.statusList["robMore"].active = true;
            for(var i=1;i<=5;i++)
                this.statusList["robMore"].getChildByName("statusRobMore"+i).active = false;
            this.statusList["robMore"].getChildByName("statusRobMore"+robNum).active = true;
        };
        this.setBet = function(score){
            this.resetStatus();
            this.statusList["betNum"].active = true;
            this.statusList["betNum"].getComponent("cc.Label").string = score+"分";
            if(score == 0)
                this.statusList["betNum"].active = false;
        };
        this.setBanker = function(){
            console.log("setBanker");
            this.statusList["banker"].active = true;
        };
        this.hideBanker = function(){
            console.log("hideBanker");
            this.statusList["banker"].active = false;
        };

        this.setLeave = function(){
            this.leaveNode.active = true;
        };
        this.hideLeave = function(){
            this.leaveNode.active = false;
        };

        this.showSay = function(durTime){
            this.sayAni.active = true;
            if(durTime)
            {
                var callFunc = function(){
                    this.sayAni.active = false;
                };
                this.scheduleOnce(callFunc,durTime);
            }
        };
        this.hideSay = function(){
            this.sayAni.active = false;
        };

        this.showSayWord = function(str){
            this.faceItem.stopAllActions();
            this.faceItem.opacity = 0;
            this.sayWord.stopAllActions();
            this.sayWord.opacity = 0;
            this.sayWordText.string = str;
            this.sayWord.width = this.sayWordTextNode.width + 100;
            this.sayWord.runAction(cc.sequence(cc.fadeIn(0.25),cc.delayTime(2),cc.fadeOut(0.5)));
        };

        this.showFace = function(index){
            this.faceItem.stopAllActions();
            this.faceItem.opacity = 0;
            this.sayWord.stopAllActions();
            this.sayWord.opacity = 0;
            this.faceItem.getComponent("cc.Sprite").spriteFrame = confige.faceFrameMap[index];
            this.faceItem.runAction(cc.sequence(cc.fadeIn(0.25),cc.delayTime(2),cc.fadeOut(0.5)));
        };
    },

    initCardWithBack:function(){
        for(var i=0;i<5;i++)
        {
            this.cardsBack[i].opacity = 255;
        }
    },
    
    setCardWithIndex : function(index, num, type){
        this.cards[index].opacity = 255;
        if(confige.roomData.roomType == "PaiGow" || confige.roomData.roomType == "PaiGowBalcony")
            this.cards[index].getComponent("cc.Sprite").spriteFrame = confige.paiGowCardFrameMap[num];
        else{
            var curCardIndex = num + type*13;
            this.cards[index].getComponent("cc.Sprite").spriteFrame = confige.bigCardFrameMap[curCardIndex];
        }

        var action2 = cc.callFunc(function() {
            var action3 = cc.scaleTo(0.2,1,1);
            this.cards[index].runAction(action3);
        }, this, 100);
        var action1 = cc.scaleTo(0.2,0,1);
        this.cardsBack[index].runAction(cc.sequence(action1,action2));
    },
    
    resetCard:function(){
        for(var i=0;i<5;i++)
        {
            this.cards[i].opacity = 0;
            this.cardsBack[i].opacity = 0;
            this.cards[i].scaleX = 0;
            this.cardsBack[i].scaleX = 1;
        }
        this.cardTypeNode.active = false;
    },

    showCardBackWithIndex:function(index){
        this.cardsBack[index].opacity = 255;
    },

    showCardBackWithCount:function(count){
        for(var i=0;i<count;i++)
            this.cardsBack[i].opacity = 255;
    },

    reset:function(){
        this.resetUserInfo();
        this.resetStatus();
        this.resetCard();
        this.statusList["banker"].active = false;
        this.leaveNode.active = false;
    },
});
